package com.example.zyfypt516wh.activities;


import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;


import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.fragment.BaseFragment;

import com.example.zyfypt516wh.fragment.CFragment1;
import com.example.zyfypt516wh.fragment.CFragment2;
import com.example.zyfypt516wh.fragment.CFragment3;


import com.example.zyfypt516wh.fragment.CFragment4;
import com.example.zyfypt516wh.fragment.CFragment5;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的收藏
 */
public class CollectActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private FragmentStateAdapter fragmentStateAdapter;
    private BottomNavigationView navigationView;
    private List<BaseFragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        initFragmentList();//初始化 FragmentList
        initViewPager();//初始化 ViewPager
        initBottomNV();//初始化 BottomNavigationView
    }

    private void initFragmentList() {
        // 将 fragment 加载到 list 中
        fragmentList = new ArrayList<>();
        fragmentList.add(new CFragment1());
        fragmentList.add(new CFragment2());
        fragmentList.add(new CFragment3());
        fragmentList.add(new CFragment4());
        fragmentList.add(new CFragment5());
    }

    private void initViewPager() {
        //实例化 viewpager
        viewPager = findViewById(R.id.viewPager2);
        //实例化 FragmentPagerAdapter
        fragmentStateAdapter = new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        };
        //设置 viewpager 的适配器
        viewPager.setAdapter(fragmentStateAdapter);
        //设置 viewpager 的页面切换事件
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                System.out.println("---position=" + position);
                switch (position) {
                    case 0:
                        navigationView.setSelectedItemId(R.id.blog);
                        break;
                    case 1:
                        navigationView.setSelectedItemId(R.id.keynote);
                        break;
                    case 2:
                        navigationView.setSelectedItemId(R.id.video);
                        break;
                    case 3:
                        navigationView.setSelectedItemId(R.id.sample);
                        break;
                    case 4:
                        navigationView.setSelectedItemId(R.id.project);
                        break;
                }
                super.onPageSelected(position);
            }
        });
    }

    //运行查看效果

    private void initBottomNV() {
        //实例化 BottomNavigationView
        navigationView = findViewById(R.id.bottomNavigationView);
        //设置 BottomNavigationView 切换事件
        navigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int id = 0;
                if (item.getItemId() == R.id.blog)
                    id = 0;
                else if (item.getItemId() == R.id.keynote)
                    id = 1;
                else if (item.getItemId() == R.id.video)
                    id = 2;
                else if (item.getItemId() == R.id.sample)
                    id = 3;
                else id = 4;
                System.out.println("---id=" + id);
                viewPager.setCurrentItem(id);
                return true;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}